package com.kuang.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //如果登录成功的话 session中会存储用户信息 读取session中的信息 判断是否登录成功
        Object username =request.getSession().getAttribute("username");

        //判断是否为空
        if(username == null){
            //用户名为空 返回登录页面
            //存储错误信息
            request.setAttribute("msg","还未登录!!!");
            request.getRequestDispatcher("/index.html").forward(request, response);
            return false;
        }else{
            //登录成功
            return true;
        }
    }
}
